package com.geeklan.smart.ui.viewmodel.device;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.geeklan.smart.data.model.ApiResponse;
import com.geeklan.smart.data.model.Device;
import com.geeklan.smart.data.model.DeviceId;
import com.geeklan.smart.data.model.DeviceList;
import com.geeklan.smart.data.model.DeviceStatusResponse;
import com.geeklan.smart.data.model.DeviceVersion;
import com.geeklan.smart.data.model.Feed;
import com.geeklan.smart.data.model.FeedPlan;
import com.geeklan.smart.data.model.FeedingRecord;
import com.geeklan.smart.data.model.RequestDevice;
import com.geeklan.smart.data.model.RequestDeviceUpdate;
import com.geeklan.smart.data.model.RequestFoodBowl;
import com.geeklan.smart.data.model.TokenResponse;
import com.geeklan.smart.data.model.UploadDeviceVideoImg;
import com.geeklan.smart.data.repository.RepositoryCallback;
import com.geeklan.smart.data.repository.device.DeviceRepository;
import com.geeklan.smart.utils.SharedPreferencesUtil;

import java.util.List;

public class DeviceViewModel extends ViewModel {
    private final DeviceRepository deviceRepository;
    private final MutableLiveData<ApiResponse<TokenResponse>> deviceLiveData = new MutableLiveData<>();
    private final MutableLiveData<ApiResponse<TokenResponse>> deviceLiveData1 = new MutableLiveData<>();
    private final MutableLiveData<DeviceList> deviceListLiveData = new MutableLiveData<>();
    private final MutableLiveData<DeviceStatusResponse> deviceStatusLiveData = new MutableLiveData<>();
    private final MutableLiveData<DeviceStatusResponse> deviceStatusUpdateLiveData = new MutableLiveData<>();
    private final MutableLiveData<ApiResponse> bindDeviceLiveData = new MutableLiveData<>();
    private final MutableLiveData<ApiResponse> feedPlanDeviceLiveData = new MutableLiveData<>();
    private final MutableLiveData<ApiResponse> feedDeviceLiveData = new MutableLiveData<>();
    private final MutableLiveData<ApiResponse> deleteUserBindDeviceLiveData = new MutableLiveData<>();
    private final MutableLiveData<ApiResponse> renameDeviceLiveData = new MutableLiveData<>();
    private final MutableLiveData<ApiResponse> clearBowlDeviceLiveData = new MutableLiveData<>();
    private final MutableLiveData<ApiResponse> sendGetLogLiveData = new MutableLiveData<>();
    private final MutableLiveData<ApiResponse> intelligentFoodBowlDeviceLiveData = new MutableLiveData<>();
    private final MutableLiveData<ApiResponse> firmwareUpgradeDeviceLiveData = new MutableLiveData<>();
    private final MutableLiveData<String> firmwareUpgradeDeviceErrorLiveData = new MutableLiveData<>();


    public MutableLiveData<DeviceStatusResponse> getDeviceStatusUpdateLiveData() {
        return deviceStatusUpdateLiveData;
    }

    public MutableLiveData<ApiResponse> getSendGetLogLiveData() {
        return sendGetLogLiveData;
    }

    public MutableLiveData<ApiResponse> getFirmwareUpgradeDeviceLiveData() {
        return firmwareUpgradeDeviceLiveData;
    }

    public MutableLiveData<String> getFirmwareUpgradeDeviceErrorLiveData() {
        return firmwareUpgradeDeviceErrorLiveData;
    }

    private final MutableLiveData<ApiResponse<List<DeviceVersion>>> deviceVersionDeviceLiveData = new MutableLiveData<>();
    private final MutableLiveData<ApiResponse<List<FeedingRecord>>> feedingRecordDeviceLiveData = new MutableLiveData<>();
    private final MutableLiveData<ApiResponse<List<String>>> getMonthFeedingRecordLiveData = new MutableLiveData<>();

    public MutableLiveData<ApiResponse<List<String>>> getGetMonthFeedingRecordLiveData() {
        return getMonthFeedingRecordLiveData;
    }

    public MutableLiveData<ApiResponse<List<DeviceVersion>>> getDeviceVersionDeviceLiveData() {
        return deviceVersionDeviceLiveData;
    }

    public MutableLiveData<ApiResponse<TokenResponse>> getDeviceLiveData1() {
        return deviceLiveData1;
    }

    public MutableLiveData<ApiResponse<List<FeedingRecord>>> getFeedingRecordDeviceLiveData() {
        return feedingRecordDeviceLiveData;
    }
    private final MutableLiveData<String> deviceVersionDeviceErrorLiveData = new MutableLiveData<>();
    private final MutableLiveData<String> errorLiveData = new MutableLiveData<>();
    private final MutableLiveData<String> bindErrorLiveData = new MutableLiveData<>();
    private final MutableLiveData<String> deviceStatusErrorLiveData = new MutableLiveData<>();
    private final MutableLiveData<String> deviceStatusUpdateErrorLiveData = new MutableLiveData<>();
    private final MutableLiveData<String> feedPlanErrorLiveData = new MutableLiveData<>();
    private final MutableLiveData<String> feedErrorLiveData = new MutableLiveData<>();
    private final MutableLiveData<String> renameErrorLiveData = new MutableLiveData<>();
    private final MutableLiveData<String> clearBowlErrorLiveData = new MutableLiveData<>();
    private final MutableLiveData<String> intelligentFoodBowlErrorDeviceLiveData = new MutableLiveData<>();
    private final MutableLiveData<String> sendGetLogErrorLiveData = new MutableLiveData<>();
    private final MutableLiveData<String> deleteUserBindDeviceErrorLiveData = new MutableLiveData<>();
    private final MutableLiveData<String> feedingRecordDeviceErrorLiveData = new MutableLiveData<>();

    public MutableLiveData<String> getIntelligentFoodBowlErrorDeviceLiveData() {
        return intelligentFoodBowlErrorDeviceLiveData;
    }

    public MutableLiveData<String> getDeviceStatusUpdateErrorLiveData() {
        return deviceStatusUpdateErrorLiveData;
    }

    public MutableLiveData<String> getSendGetLogErrorLiveData() {
        return sendGetLogErrorLiveData;
    }

    public MutableLiveData<String> getDeviceVersionDeviceErrorLiveData() {
        return deviceVersionDeviceErrorLiveData;
    }

    public MutableLiveData<String> getFeedingRecordDeviceErrorLiveData() {
        return feedingRecordDeviceErrorLiveData;
    }

    public MutableLiveData<ApiResponse> getIntelligentFoodBowlDeviceLiveData() {
        return intelligentFoodBowlDeviceLiveData;
    }

    public DeviceViewModel(DeviceRepository deviceRepository) {
        this.deviceRepository = deviceRepository;
    }

    public void getDeviceAccessToken(String deviceId, int userId) {
        RequestDevice requestDevice = new RequestDevice();
        requestDevice.setDeviceId(deviceId);
        requestDevice.setUid(userId + "");
        requestDevice.setTimeout(3600);
        deviceRepository.getDeviceAccessToken(requestDevice, new RepositoryCallback<ApiResponse<TokenResponse>>() {
            @Override
            public void onSuccess(ApiResponse<TokenResponse> data) {
                if (data != null) {
                    deviceLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                errorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void getDeviceAccessToken1(String deviceId, int userId) {
        RequestDevice requestDevice = new RequestDevice();
        requestDevice.setDeviceId(deviceId);
        requestDevice.setUid(userId + "");
        requestDevice.setTimeout(3600);
        deviceRepository.getDeviceAccessToken(requestDevice, new RepositoryCallback<ApiResponse<TokenResponse>>() {
            @Override
            public void onSuccess(ApiResponse<TokenResponse> data) {
                if (data != null) {
                    deviceLiveData1.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                errorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void getDeviceById(String deviceId) {
        RequestDevice requestDevice = new RequestDevice();
        requestDevice.setDeviceId(deviceId);
        deviceRepository.getDeviceById(requestDevice, new RepositoryCallback<DeviceStatusResponse>() {
            @Override
            public void onSuccess(DeviceStatusResponse data) {
                if (data != null) {
                    deviceStatusLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                deviceStatusErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void getDeviceByIdUpdate(String deviceId) {
        RequestDevice requestDevice = new RequestDevice();
        requestDevice.setDeviceId(deviceId);
        deviceRepository.getDeviceById(requestDevice, new RepositoryCallback<DeviceStatusResponse>() {
            @Override
            public void onSuccess(DeviceStatusResponse data) {
                if (data != null) {
                    deviceStatusUpdateLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                deviceStatusUpdateErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void getUserDevice() {
        deviceRepository.getUserDevice(new RepositoryCallback<DeviceList>() {
            @Override
            public void onSuccess(DeviceList data) {
                if (data != null) {
                    deviceListLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                errorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void bindUserDevice(String deviceId) {
        Device device = new Device();
        device.setDeviceId(deviceId);
        device.setIpAddress(SharedPreferencesUtil.getString("address_ip","0.0.0.0"));
        device.setCity(SharedPreferencesUtil.getString("address_city","广东"));
        device.setProvince(SharedPreferencesUtil.getString("address_province","深圳"));
        deviceRepository.bindUserDevice(device, new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    bindDeviceLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                bindErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void resetFeedNum(String deviceId, int feedNum1, int feedNum2) {
        FeedPlan feedPlan = new FeedPlan();
        feedPlan.setDeviceId(deviceId);
        feedPlan.setFeedNum1(feedNum1);
        feedPlan.setFeedNum2(feedNum2);
        deviceRepository.resetFeedNum(feedPlan, new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    feedPlanDeviceLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                feedPlanErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void controlOpenFeed(String deviceId, int feedType) {
        Feed feed = new Feed();
        feed.setDeviceId(deviceId);
        feed.setFeedType(feedType);
        deviceRepository.controlOpenFeed(feed, new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    feedDeviceLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                feedErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void deleteUserBindDevice(String deviceId) {
        Device device = new Device();
        device.setDeviceId(deviceId);
        deviceRepository.deleteUserBindDevice(device, new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    deleteUserBindDeviceLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                deleteUserBindDeviceErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void updateDeviceInfo(String deviceId,String deviceName) {
        Device device = new Device();
        device.setDeviceId(deviceId);
        device.setDeviceName(deviceName);
        deviceRepository.updateDeviceInfo(device, new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    renameDeviceLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                renameErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void clearBowl(String deviceId) {
        Device device = new Device();
        device.setDeviceId(deviceId);
        deviceRepository.clearBowl(device, new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    clearBowlDeviceLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                clearBowlErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void intelligentFoodBowl(String deviceId,int openState,int foodGrams) {
        RequestFoodBowl requestFoodBowl = new RequestFoodBowl();
        requestFoodBowl.setDeviceId(deviceId);
        requestFoodBowl.setFoodGrams(foodGrams);
        requestFoodBowl.setOpenState(openState);
        deviceRepository.intelligentFoodBowl(requestFoodBowl, new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    intelligentFoodBowlDeviceLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                intelligentFoodBowlErrorDeviceLiveData.setValue(e.getMessage());
            }
        });
    }

    public void firmwareUpgrade(int versionId,String deviceId) {
        RequestDeviceUpdate requestDeviceUpdate = new RequestDeviceUpdate();
        requestDeviceUpdate.setDeviceId(deviceId);
        requestDeviceUpdate.setVersionId(versionId);
        deviceRepository.firmwareUpgrade(requestDeviceUpdate, new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    firmwareUpgradeDeviceLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                firmwareUpgradeDeviceErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void uploadDeviceVideoImg(String deviceId,String imgUrl , String imgUrl1) {
        UploadDeviceVideoImg uploadDeviceVideoImg = new UploadDeviceVideoImg();
        uploadDeviceVideoImg.setDeviceId(deviceId);
        uploadDeviceVideoImg.setImgUrl(imgUrl);
        uploadDeviceVideoImg.setImgUrl1(imgUrl1);
        deviceRepository.uploadDeviceVideoImg(uploadDeviceVideoImg, new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {

                }
            }

            @Override
            public void onError(Exception e) {

            }
        });
    }

    public void getDeviceVersion() {
        deviceRepository.getDeviceVersion(new RepositoryCallback<ApiResponse<List<DeviceVersion>>>() {
            @Override
            public void onSuccess(ApiResponse<List<DeviceVersion>> data) {
                if (data != null) {
                    deviceVersionDeviceLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                deviceVersionDeviceErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void getFeedingRecordList(String deviceId,String dateDay) {
        deviceRepository.getFeedingRecordList(deviceId,dateDay, new RepositoryCallback<ApiResponse<List<FeedingRecord>>>() {
            @Override
            public void onSuccess(ApiResponse<List<FeedingRecord>> data) {
                if (data != null) {
                    feedingRecordDeviceLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                feedingRecordDeviceErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void getMonthFeedingRecord(String deviceId,String dateDay) {
        deviceRepository.getMonthFeedingRecord(deviceId,dateDay, new RepositoryCallback<ApiResponse<List<String>>>() {
            @Override
            public void onSuccess(ApiResponse<List<String>> data) {
                if (data != null) {
                    getMonthFeedingRecordLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
//                feedingRecordDeviceErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void resetDevice(String deviceId) {
        Device device = new Device();
        device.setDeviceId(deviceId);
        deviceRepository.resetDevice(device, new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    clearBowlDeviceLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                clearBowlErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void sendGetLogCommand(String id) {
        DeviceId deviceId = new DeviceId();
        deviceId.setDeviceId(id);
        deviceRepository.sendGetLogCommand(deviceId, new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    sendGetLogLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                sendGetLogErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public MutableLiveData<String> getFeedErrorLiveData() {
        return feedErrorLiveData;
    }

    public MutableLiveData<ApiResponse> getFeedDeviceLiveData() {
        return feedDeviceLiveData;
    }

    public MutableLiveData<ApiResponse> getFeedPlanDeviceLiveData() {
        return feedPlanDeviceLiveData;
    }

    public MutableLiveData<DeviceStatusResponse> getDeviceStatusLiveData() {
        return deviceStatusLiveData;
    }

    public MutableLiveData<String> getDeleteUserBindDeviceErrorLiveData() {
        return deleteUserBindDeviceErrorLiveData;
    }

    public MutableLiveData<ApiResponse> getDeleteUserBindDeviceLiveData() {
        return deleteUserBindDeviceLiveData;
    }

    public MutableLiveData<String> getDeviceStatusErrorLiveData() {
        return deviceStatusErrorLiveData;
    }

    public MutableLiveData<String> getFeedPlanErrorLiveData() {
        return feedPlanErrorLiveData;
    }

    public MutableLiveData<ApiResponse<TokenResponse>> getDeviceLiveData() {
        return deviceLiveData;
    }

    public MutableLiveData<DeviceList> getDeviceListLiveData() {
        return deviceListLiveData;
    }

    public MutableLiveData<ApiResponse> getBindDeviceLiveData() {
        return bindDeviceLiveData;
    }

    public LiveData<String> getErrorLiveData() {
        return errorLiveData;
    }

    public LiveData<String> getBindErrorLiveData() {
        return bindErrorLiveData;
    }

    public MutableLiveData<String> getRenameErrorLiveData() {
        return renameErrorLiveData;
    }

    public MutableLiveData<ApiResponse> getRenameDeviceLiveData() {
        return renameDeviceLiveData;
    }

    public MutableLiveData<String> getClearBowlErrorLiveData() {
        return clearBowlErrorLiveData;
    }

    public MutableLiveData<ApiResponse> getClearBowlDeviceLiveData() {
        return clearBowlDeviceLiveData;
    }
}
